﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }
        private int Id;
        public EditForm(int Id, String SName, int Age, int Score)
        {

            InitializeComponent();
            this.Id = Id;
            textBox1.Text = SName;
            textBox2.Text = Age.ToString();
            textBox3.Text = Score.ToString();
        }

        private void EditForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var SName = textBox1.Text;
            var Age = textBox2.Text;
            var Score = textBox3.Text;
            if (this.Id > 0)
            {
                var update = String.Format("update Students set SName='{0}',Age={1},Score={2} where Id={3}", SName, Age, Score, this.Id);
                DbHelper.AddOrUpdateOrDelete(update);
                MessageBox.Show("恭喜你，更新成功！","提示");
            }
            else
            {
                var sql = string.Format("insert into Students(SName,Age,Score)values('{0}',{1},{2})", SName, Age, Score);
                DbHelper.AddOrUpdateOrDelete(sql);
                MessageBox.Show("恭喜你，添加成功！", "提示");
            
            }
            this.DialogResult = DialogResult.Yes;
            this.Close();
          
          
        }
    }
}
